/**
 * 文件名：Mylistener.java
 *
 * 版本信息：
 * 日期：2009-6-17
 * Copyright dp Corporation 2009 
 * 版权所有 2009All rights reserved.
 * 创建者：wht
 *
 */
package com.dp.pub;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

//import com.dp.tj.JobScheduler;//去掉Quartz

/**
 * 
 * 项目名称：book 类名称：Mylistener 类描述：自定义web应用listener，目的是在应用的其他部分加载前，做一些动作
 * 动作包括定义一些系统属性、加载系统参数 创建人：Administrator 创建时间：2009-6-17 下午02:21:07
 * 修改人：Administrator 修改时间：2009-6-17 下午02:21:07 修改备注：
 * 
 * @version
 * 
 */
public class Mylistener implements ServletContextListener, HttpSessionListener,
		ServletContextAttributeListener {

	/**
	 * Constructor of the object.
	 */
	private int count;
	private ServletContext context = null;

	public Mylistener() {
		super();
		count = 0;

		if (System.getProperty("jboss.server.home.url") != null) {
			System.setProperty("my.log.dir", System.getProperty("jboss.server.log.dir"));
			System.setProperty("mydatasource", "java:myDS");
			System.setProperty("myrptdatasource", "java:myRRDS");

		} else {
			System.setProperty("my.log.dir", System.getProperty("catalina.base"));
			System.setProperty("mydatasource", "java:comp/env/jdbc/myDS");
			System.setProperty("myrptdatasource", "java:comp/env/jdbc/myRRDS");
		}
//		System.out.println(Mylistener.class);
//		System.out.println(InterUtil.rb.getString("src.jndiIs") + System.getProperty("mydatasource"));

//		new JobScheduler().createjob();//去掉Quartz
		//
	}

	// 创建一个session时激发19
	public void sessionCreated(HttpSessionEvent se) {
		count++;
		setContext(se);
	}

	// 当一个session失效时激发26
	public void sessionDestroyed(HttpSessionEvent se) {
		count--;
		setContext(se);
	}// 设置context的属性，它将激发attributeReplaced或attributeAdded方法32

	public void setContext(HttpSessionEvent se) {
		se.getSession().getServletContext().setAttribute("onLine",
				new Integer(count));
	}

	// 增加一个新的属性时激发37
	public void attributeAdded(ServletContextAttributeEvent event) {
		log("attributeAdded('" + event.getName() + "', '" + event.getValue()
				+ "')");
	}

	// 删除一个新的属性时激发45
	public void attributeRemoved(ServletContextAttributeEvent event) {
		log("attributeRemoved('" + event.getName() + "',    '"
				+ event.getValue() + "')");
	}// 属性被替代时激发53

	public void attributeReplaced(ServletContextAttributeEvent event) {
		log("attributeReplaced('" + event.getName() + "', '" + event.getValue()
				+ "')");
	}

	// context删除时激发59
	public void contextDestroyed(ServletContextEvent event) {
		log("contextDestroyed()");
		this.context = null;
	}

	// context初始化时激发67
	public void contextInitialized(ServletContextEvent event) {
		this.context = event.getServletContext();
		context.setAttribute("my.log.dir", System.getProperty("my.log.dir"));
		context.setAttribute("mydatasource", System.getProperty("mydatasource"));
		log("contextInitialized()");
	}

	private void log(String message) {
		System.out.println("ContextListener: " + message);
	}

	// public void contextDestroyed(ServletContextEvent sce) {
	// // TODO Auto-generated method stub
	//		
	// }
	//
	// public void contextInitialized(ServletContextEvent sce) {
	// // TODO Auto-generated method stub
	//		
	// }
	//
	// public void sessionCreated(HttpSessionEvent se) {
	// // TODO Auto-generated method stub
	//		
	// }
	//
	// public void sessionDestroyed(HttpSessionEvent se) {
	// // TODO Auto-generated method stub
	//		
	// }
	//
	// public void attributeAdded(ServletContextAttributeEvent scab) {
	// // TODO Auto-generated method stub
	//		
	// }
	//
	// public void attributeRemoved(ServletContextAttributeEvent scab) {
	// // TODO Auto-generated method stub
	//		
	// }
	//
	// public void attributeReplaced(ServletContextAttributeEvent scab) {
	// // TODO Auto-generated method stub
	//		
	// }

}
